package com.eshin.atuoconfig;

import java.beans.PropertyVetoException;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.mchange.v2.c3p0.ComboPooledDataSource;

//@Configuration 
@EnableAutoConfiguration
//启注解事务管理  
@EnableTransactionManagement 
@MapperScan(basePackages="com.eshin.mapper2",
sqlSessionFactoryRef = "sqlSessionFactoryBean2")
//@ConfigurationProperties(prefix="spring.datasource") 
public class DatasourceConfig2 {
	@Resource(name="datasourceProperties2")
	private DatasourceProperties2 datasourceProperties2;
	//DataSource配置
    @Bean(name="dataSource2")
    public ComboPooledDataSource dataSource2() throws PropertyVetoException {
    	System.out.println("dataSource2");
    	ComboPooledDataSource dataSource = new ComboPooledDataSource();
    	dataSource.setJdbcUrl(datasourceProperties2.getUrl());
    	dataSource.setUser(datasourceProperties2.getUsername());
    	dataSource.setPassword(datasourceProperties2.getPassword());
    	dataSource.setDriverClass(datasourceProperties2.getDriver());
        return dataSource;
    }
    
    //DataSource配置
//    @Bean
//    @ConfigurationProperties(prefix="spring.datasource")
//    public DataSource dataSource() {
//        return new org.apache.tomcat.jdbc.pool.DataSource();
//    }
  //提供SqlSeesion
    @Bean(name="sqlSessionFactoryBean2")
    public SqlSessionFactory sqlSessionFactoryBean2(@Qualifier("dataSource2")ComboPooledDataSource dataSource2) throws Exception {
    	
    	SqlSessionFactoryBean sqlSessionFactoryBean2 = new SqlSessionFactoryBean();
    	sqlSessionFactoryBean2.setDataSource(dataSource2);
    	
//    	PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//    	
//        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:com/eshin/mapper/*.xml"));
//    	System.out.println("sqlSessionFactoryBean2:"+sqlSessionFactoryBean2.getObject());
    	return sqlSessionFactoryBean2.getObject();
    }
    
    @Bean
    public PlatformTransactionManager transactionManager2() throws PropertyVetoException {
    	return new DataSourceTransactionManager(dataSource2());
    }
    
    @Bean
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactoryBean2")SqlSessionFactory sqlSessionFactoryBean ){
    	return new SqlSessionTemplate(sqlSessionFactoryBean);
    }
}
